﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Profile;
using System.Text;

using Gift.DAL;
using Gift.Model;

namespace WebSite.cn
{
    public partial class category : System.Web.UI.UserControl
    {
        // Fields
        public string typemsg = "";
        readonly BasePage theBasePage = new BasePage();
       readonly  DALProfessionAreaType theAreaType = new DALProfessionAreaType();

        private string sBigType
        {
            get
            {
                return Request.QueryString["sid"];
            }
        }

        // Methods
        protected void Page_Load(object sender, EventArgs e)
        {
                string sql = string.Empty;;
                string sType = string.Empty;

                if (sBigType == "2") //专区
                {


                    StringBuilder sbHtml = new StringBuilder();
                    StringBuilder sbHtmlTable;
                    List<tbProfessionAreaType> lstType = theAreaType.GetList();

                    sbHtmlTable = new StringBuilder();
                    sbHtmlTable.Append("<table  class='cartgory_con_table' cellpadding='0' width='100%' cellspacing='0'><tr><td class='cartgory_con_left' style='width:80px;'>");
                    this.typemsg = "<table border='0' cellpadding='0' cellspacing='0' width='100%' style='background-color:#FFF;'><tr>";
                    int iNum = 0;
                    string sCurrentClass = "";
                    string sClassName = string.Empty;
                    if (Request.QueryString["typeid"] == "-1" || Request.QueryString["typeid"] == null)
                    {
                        sCurrentClass = "CurrentType";
                        string sTable = sbHtmlTable.Replace("cartgory_con_table", "cartgory_con_current").ToString();
                        sbHtmlTable = new StringBuilder();
                        sbHtmlTable.Append(sTable);
                    }

                    sClassName = lstType.Find(m => m.Id == -1).Name;
                    sbHtmlTable.Append("<a href='professionArea.aspx?sid=" + Request.QueryString["sid"] + "&typeid=-1'>" + sClassName + "</a></td><td>");

                    iNum++;
                    foreach (tbProfessionAreaType oType in lstType)
                    {
                        if (oType.Id == -1) continue;
                        sCurrentClass = "";
                        if (Request.QueryString["typeid"] == oType.Id.ToString())
                        {
                            sCurrentClass = "CurrentType";
                            string sTable = sbHtmlTable.Replace("cartgory_con_table", "cartgory_con_current").ToString();
                            sbHtmlTable = new StringBuilder();
                            sbHtmlTable.Append(sTable);
                        }
                        this.typemsg = this.typemsg + "<td width='17%' class='" + sCurrentClass + "'><a href='professionArea.aspx?sid=" + Request.QueryString["sid"] + "&typeid=" + oType.Id + "'>" + oType.Name + "</a></td>";


                        if (iNum % 6 == 0)
                        {
                            this.typemsg = this.typemsg + "</tr><tr>";
                        }
                        iNum++;

                    }
                    if ((lstType.Count-1) % 7 != 0)
                    {
                        this.typemsg = this.typemsg + string.Format("<td colspan='{0}'></td>", (7 - (lstType.Count - 1)).ToString());
                    }

                    this.typemsg = this.typemsg + "</tr></table>";
                    sbHtmlTable.Append(this.typemsg + "</td></tr></table>");

                    sbHtml.Append(sbHtmlTable);

                    this.typemsg = sbHtml.ToString();
                    return;
                }

                else if (sBigType == "3")
                {
                    sType = " and proisnew3 = 1";
                }
                else if (sBigType == "4")
                {
                    sType = " and proisnew4 = 1";
                }

                else
                {
                    sType = " and (proisnew3 = 1 or proisnew4 =1) ";
                }
                sql = "select distinct classid,classname,parentid,classorder from DB_Productclass where  visible=1 and parentid = 0  "
                    + " and classid in (select PC.parentid from DB_Productclass PC join DB_Shoppro S on PC.classid = S.typeid and PC.parentid !=0 and PC.visible=1 " + sType +")"
                    + " and  parentid=0 and visible=1 order by classorder";
         
                DataTable table = SQLAccess.GreatDs(sql).Tables[0];
                if (table.Rows.Count > 0)
                {
                    StringBuilder sbHtml = new StringBuilder();
                    StringBuilder sbHtmlTable ;
                    int iRowIndex = 0;
                    string sTopName = "tbMore0";
                    string sDisplay = "style='display:inline;'";
                    int iMaxIndex = 0;
                    foreach (DataRow row in table.Rows)
                    {
                        iMaxIndex++;
                        sbHtmlTable = new StringBuilder();
                        sbHtmlTable.AppendFormat("<table class='cartgory_con_table' cellpadding='0' width='100%' cellspacing='0'><tr><td class='cartgory_con_left' style='width:80px;'>");
                        DataTable table2 = SQLAccess.GreatDs("select distinct PC.classid,PC.classname,PC.parentid,PC.classorder from DB_Productclass PC join DB_Shoppro S on PC.classid=S.typeid " + sType + " where PC.parentid=" + row["classid"].ToString() + " and PC.visible=1 order by PC.classorder").Tables[0];
                        if (table2.Rows.Count > 0)
                        {

                            this.typemsg = "<table border='0' cellpadding='0' cellspacing='0' width='100%' style='background-color:#FFF;'><tr>";
                            int iNum = 0;
                            string sCurrentClass = "";
                            string sClassName = string.Empty;
                            if (Request.QueryString["typeid"] == row["classid"].ToString())
                            {
                                sCurrentClass = "CurrentType";
                                string sTable = sbHtmlTable.Replace("cartgory_con_table", "cartgory_con_current").ToString();
                                sbHtmlTable = new StringBuilder();
                                sbHtmlTable.Append(sTable);
                            }
                            sClassName =  row["classname"].ToString();
                            sbHtmlTable.Append("<a href='hotpro.aspx?sid=" + Request.QueryString["sid"] + "&typeid=" + row["classid"].ToString() + "'>" + sClassName + "</a></td><td>");

                            iNum++;
                            iRowIndex = 0;
                            sDisplay = "style='display:inline;'";
                            bool isHave = false;
                            foreach (DataRow row2 in table2.Rows)
                            {
                              
                                sCurrentClass = "";
                               
                                if (Request.QueryString["typeid"] == row2["classid"].ToString())
                                {
                                    sCurrentClass = "CurrentType";
                                    string sTable = sbHtmlTable.Replace("cartgory_con_table", "cartgory_con_current").ToString();
                                    sbHtmlTable = new StringBuilder();
                                    sbHtmlTable.Append(sTable);
                                    isHave = true;
                                  
                                }
                                if (iRowIndex > 5)
                                {
                                    sTopName = iMaxIndex + "_" + iRowIndex.ToString();
                                    sDisplay = "style='display:none;'";
                                }

                                this.typemsg = this.typemsg + string.Format( "<td   id='{0}' {1} width='17%' class='" + sCurrentClass + "'><a href='hotpro.aspx?sid=" + Request.QueryString["sid"] + "&typeid=" + row2["classid"].ToString() + "'>" + row2["classname"].ToString() + "</a></td>",sTopName,sDisplay);
                                iRowIndex++;

                                if (iNum % 6 == 0)
                                {
                                    this.typemsg = this.typemsg + "</tr><tr>";
                                }
         
                                iNum++;
                            }
                            if (isHave)
                            {
                                this.typemsg = this.typemsg.Replace("style='display:none;", "style='display:inline;");
                            }
                           
                            if (table2.Rows.Count % 7 != 0)
                            {
                                this.typemsg = this.typemsg + string.Format("<td colspan='{0}'></td>",(7-table2.Rows.Count).ToString());
                            }
                           
                            this.typemsg = this.typemsg + "</tr>";
                            if (table2.Rows.Count > 5)
                            {

                                typemsg = this.typemsg + ("<tr><td class='td_top_more' colspan='5'><a onclick='setMore(" + iMaxIndex + "," + iRowIndex + ")' style='cursor:pointer;'><div id='divViewMore" + iMaxIndex + "'>显示更多...</div></td></tr>");

                            }
                            this.typemsg = this.typemsg + "</table>";
                           
                            sbHtmlTable.Append(this.typemsg + "</td></tr></table>");
                        }

                      
                        sbHtml.Append(sbHtmlTable);
                    }
                   
                    this.typemsg = sbHtml.ToString();
  
            }
        }



    }
}